home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / c / bor_ti.exe / TI1160.ASC < prev    next >
Text File  |  1992-12-03  |  21KB  |  793 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.   PRODUCT  :  Borland C++                           NUMBER  :  1160
  9.   VERSION  :  3.1
  10.        OS  :  DOS
  11.      DATE  :  December 3, 1992                        PAGE  :  1/12
  12.  
  13.     TITLE  :  Dynamically modifying Turbo Vision menus.
  14.  
  15.  
  16.  
  17.  
  18.        This document provides source code to an example which
  19.   dynamically modifies menus using Turbo Vision for C++.
  20.  
  21.  
  22.   /***********************************************************************
  23.    *                                                                     *
  24.    * MMENU.CPP                                                           *
  25.    *   This module contains the code to support the TMultiMenu class.    *
  26.    *                                                                     *
  27.    ***********************************************************************/
  28.  
  29.   #define Uses_TEvent
  30.   #define Uses_TMenu
  31.   #define Uses_TSubMenu
  32.   #define Uses_TMenuItem
  33.   #define Uses_TMenuBar
  34.   #include <tv.h>
  35.  
  36.   #if !defined( __MMENU_H )
  37.   #include "mmenu.h"
  38.   #endif
  39.  
  40.  
  41.   /***********************************************************************
  42.    * global operator +
  43.    *
  44.    * Since the objects will always be in a linked list, and the operator+
  45.    * is processd left-to-right, we will define the function as appending
  46.    * menuItem2 to menuItem1, and then return menuItem1.
  47.    ***********************************************************************/
  48.  
  49.   TMenuItem& operator +( TMenuItem& menuItem1, TMenuItem& menuItem2 )
  50.   {
  51.       TMenuItem *p = &menuItem1;
  52.       while( p->next != NULL )
  53.           p = p->next;
  54.       p->next = &menuItem2;
  55.       return menuItem1;
  56.   }
  57.  
  58.  
  59.   /***********************************************************************
  60.    *                                                                     *
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.   PRODUCT  :  Borland C++                           NUMBER  :  1160
  75.   VERSION  :  3.1
  76.        OS  :  DOS
  77.      DATE  :  December 3, 1992                        PAGE  :  2/12
  78.  
  79.     TITLE  :  Dynamically modifying Turbo Vision menus.
  80.  
  81.  
  82.  
  83.  
  84.    * class TTestList                                                     *
  85.    *                                                                     *
  86.    ***********************************************************************
  87.    * TMultiMenu::TMultiMenu
  88.    *   Constructor for a TMultiMenu object.  This version takes an array
  89.    *   of TMenu pointers.
  90.    ***********************************************************************/
  91.  
  92.   TMultiMenu::TMultiMenu( const TRect& bounds, TMenu *aMenu[],
  93.               int nMenus ) :    TMenuBar( bounds, aMenu[0] ),
  94.               mList( new TMenu *[nMenus] )
  95.   {
  96.       if( nMenus == 0)
  97.           for( nMenus = 0; aMenu[nMenus] != NULL; nMenus++ )
  98.               ;
  99.       numMenus = nMenus;
  100.  
  101.       for( int i = 0; i < nMenus; i++ )
  102.           mList[i] = aMenu[i];
  103.   }
  104.  
  105.  
  106.   /***********************************************************************
  107.    * TMultiMenu::TMultiMenu
  108.    *   Constructor for a TMultiMenu object.  This version takes an array
  109.    *   of TSubMenu objects.
  110.    ***********************************************************************/
  111.  
  112.   TMultiMenu::TMultiMenu( const TRect& bounds, TSubMenu aMenu[],
  113.                           int nMenus ) :
  114.       TMenuBar( bounds, aMenu[0] ),
  115.       numMenus( nMenus ),
  116.       mList( new TMenu *[nMenus] )
  117.   {
  118.       mList[0] = menu;                  // First menu is already allocated.
  119.       for( int i = 1; i < nMenus; i++ )
  120.           mList[i] = new TMenu( aMenu[i] );
  121.   }
  122.  
  123.  
  124.   /***********************************************************************
  125.    * TMultiMenu::~TMultiMenu
  126.    *   Destructor for a TMultiMenu object.  Destroys any stored menus
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.   PRODUCT  :  Borland C++                           NUMBER  :  1160
  141.   VERSION  :  3.1
  142.        OS  :  DOS
  143.      DATE  :  December 3, 1992                        PAGE  :  3/12
  144.  
  145.     TITLE  :  Dynamically modifying Turbo Vision menus.
  146.  
  147.  
  148.  
  149.  
  150.    *   except for the current one (which will be destroyed by ~TMenuBar)
  151.    *   and frees the space where the list was stored.
  152.    ***********************************************************************/
  153.  
  154.   TMultiMenu::~TMultiMenu()
  155.   {
  156.       for( int i = 0; i < numMenus; i++ )
  157.           if( mList[i] != menu )          // Delete all but current menu.
  158.               delete mList[i];
  159.  
  160.       delete [] mList;
  161.   }
  162.  
  163.  
  164.   /***********************************************************************
  165.    * TMultiMenu::handleEvent
  166.    *   Code to respond to the cmMMChangeMenu broadcast message.  The
  167.    *   data the arrives with this message specifies which menu to switch
  168.    *   to, passed via the infoInt data member of TEvent.
  169.    ***********************************************************************/
  170.  
  171.   void TMultiMenu::handleEvent( TEvent& event )
  172.   {
  173.       if( event.what == evBroadcast &&
  174.           event.message.command == cmMMChangeMenu )
  175.       {
  176.           if( event.message.infoInt >= 0 &&
  177.               event.message.infoInt < numMenus )
  178.           {
  179.               if( menu != mList[ event.message.infoInt ] )
  180.               {
  181.                   menu = mList[ event.message.infoInt ];
  182.                   drawView();
  183.               }
  184.           }
  185.           clearEvent( event );
  186.       }
  187.       else
  188.           TMenuBar::handleEvent( event );
  189.   }
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.   PRODUCT  :  Borland C++                           NUMBER  :  1160
  207.   VERSION  :  3.1
  208.        OS  :  DOS
  209.      DATE  :  December 3, 1992                        PAGE  :  4/12
  210.  
  211.     TITLE  :  Dynamically modifying Turbo Vision menus.
  212.  
  213.  
  214.  
  215.  
  216.   /***********************************************************************
  217.    *                                                                     *
  218.    * TEST.CPP                                                            *
  219.    *   This module contains the Turbo Vision application code to run     *
  220.    *   this example.  It sets up the necessary menus to bring up the     *
  221.    *   test module represented by this demo.                             *
  222.    *                                                                     *
  223.    * TEST MODULE for Multiple Menu Bar Demo.                             *
  224.    *                                                                     *
  225.    ***********************************************************************
  226.    *                                                                     *
  227.    * This code was written by Borland Technical Support.                 *
  228.    * It is provided as is with no warranties expressed or implied.       *
  229.    *                                                                     *
  230.    ***********************************************************************/
  231.  
  232.   #define Uses_TRect
  233.   #define Uses_TKeys
  234.   #define Uses_TEvent
  235.   #define Uses_TDialog
  236.   #define Uses_TMenu
  237.   #define Uses_TMenuItem
  238.   #define Uses_TMenuBar
  239.   #define Uses_TDeskTop
  240.   #define Uses_TProgram
  241.   #define Uses_TApplication
  242.   #include <tv.h>
  243.  
  244.   #pragma hdrstop
  245.  
  246.   #if !defined( __CMDS_H )
  247.   #include "cmds.h"
  248.   #endif
  249.  
  250.   #if !defined( __MMENU_H )
  251.   #include "mmenu.h"
  252.   #endif
  253.  
  254.  
  255.   /***********************************************************************
  256.    *
  257.    * Application object for demo.
  258.    *
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.   PRODUCT  :  Borland C++                           NUMBER  :  1160
  273.   VERSION  :  3.1
  274.        OS  :  DOS
  275.      DATE  :  December 3, 1992                        PAGE  :  5/12
  276.  
  277.     TITLE  :  Dynamically modifying Turbo Vision menus.
  278.  
  279.  
  280.  
  281.  
  282.    ***********************************************************************/
  283.   class TTestApp : public TApplication
  284.   {
  285.  
  286.   public:
  287.  
  288.       TTestApp();
  289.       static TMenuBar *initMenuBar( TRect r );
  290.       virtual void handleEvent( TEvent& event );
  291.  
  292.   protected:
  293.  
  294.       int curMenu;
  295.  
  296.   };
  297.  
  298.  
  299.   /***********************************************************************
  300.    *
  301.    * TTestApp::TTestApp()
  302.    *
  303.    * Application object contructor.
  304.    *
  305.    ***********************************************************************/
  306.   TTestApp::TTestApp() :